import { customTable } from "@/components/ProTable/interface";
import { searchFormProps } from "@/components/SearchForm/interface";
import { BasicSearchForm, ProTableValue, ProTableValue2 } from "@/interface/public";

/**
 * @param {Array} dataGrid 搜索的表格
 * @param {Ref} proTableBox 表格的ref
 * @param {string} queryField 搜索的字段(多个以逗号分隔)
 * @param {string} placeholder 提示站位符
 * @param {string} type input框类型
 * @description 自定义搜索条件
 */
export const useCustomSearchForm = (
  dataGrid: customTable,
  proTableBox: ProTableValue | ProTableValue2,
  queryField: string = "netWeight,packageNum",
  placeholder: string = "请输入净量或包装编号",
  type: "number" | "daterange" | "datetimerange" | "datetime" | "digit" | "text" = "digit",
  tableType: 1 | 2 = 1
) => {
  const searchForm = reactive<searchFormProps<BasicSearchForm>>({
    form: {
      weight: "",
      id: ""
    },
    options: [
      {
        type: "input",
        label: "",
        prop: "weight",
        first: true,
        hide: false,
        defaultValue: "",
        attrs: {
          placeholder: placeholder,
          type: type
        },
        inputMethod: value => {
          dataGrid.useData = !!value;
          if (tableType === 1) {
            dataGrid.data = filterDataGrid(proTableBox.value.tableData, queryField, value);
          } else if (tableType === 2) {
            dataGrid.data = filterDataGrid2(proTableBox.value.tableData, queryField, value);
          }
        }
      }
    ]
  });

  watch(
    () => proTableBox.value?.tableData,
    () => {
      dataGrid.useData = false;
      setTimeout(() => {
        searchForm.form.weight = "";
      }, 50);
    }
  );
  return {
    searchForm
  };
};
